package com.demo.util;

import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Objects;

public class AnnotationUtilsTest {

    @Test
    public void test() {
        Class<HelloController> beanClass = HelloController.class;
        RestController annotation = AnnotationUtils.getAnnotation(beanClass, RestController.class);
        System.err.println("base url: " + (Objects.nonNull(annotation) ? annotation.value() : ""));
        for (Method method : beanClass.getMethods()) {
            boolean hasAnnotation = AnnotatedElementUtils.hasAnnotation(method, GetMapping.class);
            System.out.println(method.getName() + " has @GetMapping: " + hasAnnotation);
        }
    }

    @RestController("/api")
    static class HelloController {
        @GetMapping("/hello")
        public String hello() {
            return "Hello";
        }
        @GetMapping("/world")
        public String world() {
            return "World";
        }
        @PostMapping
        public String post() {
            return "Post";
        }
    }

}
